package com.zsc.newbuds.controller;

import com.zsc.newbuds.entity.ExchangeRecord;
import com.zsc.newbuds.entity.User;
import com.zsc.newbuds.service.impl.ExchangeRecordServiceImpl;
import com.zsc.newbuds.service.impl.UserServiceImpl;
import com.zsc.newbuds.utils.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.relational.core.sql.In;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author D
 */
@Controller
public class UserController {

    @Autowired
    UserServiceImpl userService;

    @Autowired
    RedisUtil redisUtil;

    @Autowired
    ExchangeRecordServiceImpl exchangeRecordService;

    @PostMapping("/userInfo")
    public String changeUserInfo(@RequestParam("userId") Integer userId,
                                 @RequestParam("username")String username,
                                 @RequestParam("province")String province,
                                 @RequestParam("city") String city,
                                 @RequestParam("county") String county,
                                 @RequestParam("detailAddress") String detailAddress,
                                 HttpSession session){

        //获取前session中user的值
        User user=(User)session.getAttribute("user");
        //System.out.println(user);

        //将完整地址拼接
        String address= province + city + county + detailAddress;
        userService.updateUserInfo(userId,username,address);

        //更新修改后的session中User对应的值
        user.setUserName(username);
        user.setAddress(address);
        System.out.println(user);
        session.setAttribute("user",user);
        return "redirect:main/index.html";
    }

    @PostMapping("/userChangePassword")
    public String userChangePwd(@RequestParam("pwd") String password,
                                @RequestParam("newpwd") String newpassword,
                                HttpSession session, HttpServletRequest request){
        User user=(User)session.getAttribute("user");
        System.out.println(user);
        Integer userId=user.getUserId();
        userService.updateUserPassword(userId,newpassword);
        session.removeAttribute("user");
        return "redirect:main/index.html";
    }

    @ResponseBody
    @PostMapping("/changePhoneNumber")
    public int userChangePhone(String phone,String code, HttpSession session){
        User user=(User)session.getAttribute("user");
        Integer userId=user.getUserId();
        //System.out.println("phone="+phone+" Code="+code);

        int flag = 1;
        //获取redis中的验证码进行匹配
        String redisCode = (String)redisUtil.get(phone);
        //System.out.println("redisCode:"+redisCode);
        //相同则修改用户手机号
        if(code.equals(redisCode)){
            userService.updateUserPhoneNumber(userId,phone);
            user.setPhone(phone);
            session.setAttribute("user",user);
        }
        else {
            //System.out.println("验证码不正确");
            flag = 0;
        }
        return flag;
    }
    @ResponseBody
    @GetMapping("user/getAddress")
    public String getAddress(HttpSession session){
        User user = (User)session.getAttribute("user");
        return user.getAddress();
    }

    @ResponseBody
    @GetMapping("admin/userManagement/getUserList")
    public List<User> getUserList(){
        return userService.getUserList();
    }

    @ResponseBody
    @PostMapping("admin/userManagement/deleteUser")
    public void deleteUser(Integer userId){
        userService.deleteUser(userId);
    }

}
